# arrgh - everything I found on the web about makefile rules for the
# arduino/esp environment was bogus.. ended-up up looking at the debug
# output from the Arduino IDE..

SRC=kissemulator.c

BASE=$(shell pwd)
BUILDPATH=$(BASE)/.build
CACHEPATH=$(BASE)/.cache
PACKAGES=$(HOME)/.arduino15/packages
TOOLS=$(PACKAGES)/esp8266/tools
OBJ=$(BUILDPATH)/$(SRC).bin

FQBN=esp8266:esp8266:nodemcuv2:xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,led=2,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200
IDE_VER=10813

all:
	arduino-builder -compile -verbose \
	-hardware /usr/share/arduino/hardware -hardware $(PACKAGES) \
	-tools /usr/share/arduino/hardware/tools/avr -tools $(PACKAGES) \
	-libraries $(HOME)/Arduino/libraries \
	-fqbn=$(FQBN) -ide-version=$(IDE_VER) -warnings=default \
	-build-path $(BUILDPATH) -build-cache $(CACHEPATH) \
	\
	-prefs=build.warn_data_percentage=75 \
	-prefs=runtime.tools.xtensa-lx106-elf-gcc.path=$(TOOLS)/xtensa-lx106-elf-gcc/3.0.0-newlib4.0.0-gnu23-48f7b08 \
	-prefs=runtime.tools.xtensa-lx106-elf-gcc-3.0.0-newlib4.0.0-gnu23-48f7b08.path=$(TOOLS)/xtensa-lx106-elf-gcc/3.0.0-newlib4.0.0-gnu23-48f7b08 \
	-prefs=runtime.tools.python3.path=$(TOOLS)/python3/3.7.2-post1 \
	-prefs=runtime.tools.python3-3.7.2-post1.path=$(TOOLS)/python3/3.7.2-post1 \
	-prefs=runtime.tools.mkspiffs.path=$(TOOLS)/mkspiffs/3.0.0-newlib4.0.0-gnu23-48f7b08 \
	-prefs=runtime.tools.mkspiffs-3.0.0-newlib4.0.0-gnu23-48f7b08.path=$(TOOLS)/mkspiffs/3.0.0-newlib4.0.0-gnu23-48f7b08 \
	$(SRC)

flash:
	python3 $(PACKAGES)/esp8266/hardware/esp8266/3.0.0/tools/upload.py \
	--chip esp8266 \
	--port /dev/ttyUSB0 --baud 460800 \
	--before default_reset --after hard_reset \
	write_flash 0x0 $(OBJ)

clean:
	rm -rf $(BUILDPATH) $(CACHEPATH)
	mkdir $(BUILDPATH) $(CACHEPATH)
